# -*- coding: utf-8 -*-

'''eof
name:申请人与税务信息法定代表人一致性校验
code:R07_EG_TaxInformationCorporationCheck_GZ
tableName:
columnName:
groups:企业业务信息校验
dependencies:TX_GZ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        eg = TX_GZ_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def getRule07():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        basic_list = FormatData("syptSwdjxx")
        legalPerson = ratingRequest.get("legalPerson")
        legalPersonIdCard = ratingRequest.get("legalPersonIdCard")
        if basic_list == [{}]:
            raise RuntimeError("未能获取到税务信息")
        else:
            if len(basic_list) >= 1:
                for i in basic_list:
                    FDDBRHFZRHYZXM = i.get("FDDBRHFZRHYZXM")
                    FDDBRHFZRHYZSFZJHM = i.get("FDDBRHFZRHYZSFZJHM")
                    if legalPerson not in null_type_list and legalPersonIdCard not in null_type_list and \
                            FDDBRHFZRHYZXM not in null_type_list and FDDBRHFZRHYZSFZJHM not in null_type_list:
                        if str.strip(legalPerson.encode()) == str.strip(FDDBRHFZRHYZXM.encode()) and \
                                str.strip(legalPersonIdCard.encode()) == str.strip(FDDBRHFZRHYZSFZJHM.encode()):
                            return 1
                        else:
                            return 0
                    else:
                        raise ValueError("税务信息（法人姓名，身份证号为空）FDDBRHFZRHYZXM = " + FDDBRHFZRHYZXM +
                                           ";FDDBRHFZRHYZSFZJHM = " + FDDBRHFZRHYZSFZJHM)
            else:
                raise RuntimeError("税务信息为空")
    except:
        raise


if __name__ == "__main__":
    ratingRequest = {"legalPerson": u"舒十六", "legalPersonIdCard": "510102196706108511"}
    file_obj = open(r'../data/rating/R07_EG_TaxInformationCorporationCheck_GZ/TX_CQ_DSJ.json', 'r')
    content = file_obj.read()
    TX_GZ_DSJ = json.loads(content, strict=False)
    aa = getRule07()
    print aa

result = getRule07()

